/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminImageDialog.java
 *
 * Created on May 14, 2012, 8:50:06 PM
 */
package GUI.Admin;

import java.awt.Dimension;
import java.io.File;
import javax.swing.ActionMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import pojo.Hinhanh;
import pojo.Phim;

/**
 *
 * @author Hnmtuan
 */
public class AdminImageDialog extends javax.swing.JDialog {

    // <editor-fold defaultstate="collapsed" desc="Define all variables using this application">
    private Hinhanh _ownedImage;
    private Phim _ownedFilm;
    private File _imageFile;
    private ActionMap _rsActionMap;
    private JFileChooser _fileChooser;
    private int _actionResult;
    private int _updateState;
    private String _codeImage;
    public final static int YES_OPTION = 1;
    public final static int CANCEL_OPTION = 0;
    public final static int DELETE_OPTION = 2;
    public final static int UPDATE_STATE = 1;
    public final static int NON_UPDATE_STATE = 0;
    private final int PREFERED_WIDTH = 390;
    private final int PREFERED_HEIGHT = 350;
    // </editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Define messages and error messages" >
    private final String MSG_EMPTY = "";
    private final String ERR_INVALID_PARAMS = "Invalid parameters !";
    private final String MSG_DELETE_CONFIRM = "Do you want to delete %s ?";
    private final String MSG_DELETE_TITLE = "Delete image ";
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Define Constructor" >

    public AdminImageDialog(java.awt.Frame parent, boolean modal, Hinhanh paramImage) throws Exception {
        super(parent, modal);
        _ownedImage = paramImage;
        initComponents();
        configComponents();
        initActions();
        loadImage();
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Define Init Actions" >
    private void initActions() throws Exception {
        _rsActionMap = Application.getInstance(main.MainFormApp.class).getContext().getActionMap(getClass(), this);
        _actionResult = CANCEL_OPTION;
        _updateState = (_ownedImage != null) ? UPDATE_STATE : NON_UPDATE_STATE;
        jbtnBrowse.setAction(_rsActionMap.get("browseFile"));
        jbtnSave.setAction(_rsActionMap.get("saveFile"));
        jbtnCancel.setAction(_rsActionMap.get("cancelFile"));
        jbtnDelete.setAction(_rsActionMap.get("deleteFile"));

    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement All Property or State">

    public boolean isDeletedImage() {
        return _ownedImage != null;
    }

    public int getUpdateState() {
        return _updateState;
    }

    public void setUpdateState(int _updateState) {
        if (_ownedImage == null) {
            this._updateState = NON_UPDATE_STATE;
            return;
        }
        this._updateState = _updateState;
    }

    public String getCodeImage() {
        return _codeImage;
    }

    public int getActionResult() {
        return _actionResult;
    }

    public Hinhanh getOwnedImage() {
        return _ownedImage;
    }

    public void setOwnedImage(Hinhanh _ownedImage) {
        this._ownedImage = _ownedImage;
    }

    public File getImageFile() {
        return _imageFile;
    }

    public void setImageFile(File _imageFile) {
        this._imageFile = _imageFile;
    }

    public Phim getOwnedFilm() {
        return _ownedFilm;
    }

    public void setOwnedFilm(Phim _ownedFilm) {
        this._ownedFilm = _ownedFilm;
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement All Actions">

    @Action(enabledProperty = "DeletedImage")
    public void deleteFile() throws Exception {
        String msg = String.format(MSG_DELETE_CONFIRM, _ownedImage.getCodeHinhAnh());
        int returnOptionDialog = JOptionPane.showConfirmDialog(this, msg, MSG_DELETE_TITLE, JOptionPane.OK_CANCEL_OPTION);
        if (returnOptionDialog != JOptionPane.YES_OPTION) {
            return;
        }
        _actionResult = DELETE_OPTION;
        this.setVisible(false);
    }

    @Action
    public void browseFile() throws Exception {
        if (_fileChooser == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        int resultFileChooser = _fileChooser.showOpenDialog(this);
        if (resultFileChooser != JFileChooser.APPROVE_OPTION) {
            return;
        }
        _imageFile = _fileChooser.getSelectedFile();
        String urlOfFile = _imageFile.getAbsolutePath();
        jtfURLImage.setText(urlOfFile);
    }

    @Action
    public void saveFile() {
        _actionResult = YES_OPTION;
        _codeImage = jtfCodeImage.getText();
        this.setVisible(false);
    }

    @Action
    public void cancelFile() {
        _actionResult = CANCEL_OPTION;
        this.setVisible(false);
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement Actions to GUI" >

    protected final void configComponents() {
        this.setPreferredSize(new Dimension(PREFERED_WIDTH, PREFERED_HEIGHT));
        _fileChooser = (_fileChooser != null) ? _fileChooser : new JFileChooser();
        _fileChooser.setFileFilter(new FileNameExtensionFilter("JPG & GIF & PNG Images", "jpg", "gif", "png"));
    }

    protected final void refreshAllFields() {
        jtfCodeImage.setText(MSG_EMPTY);
        jtfURLImage.setText(MSG_EMPTY);
    }

    protected final void loadImage() {
        if (_ownedImage == null) {
            refreshAllFields();
            firePropertyChange("DeletedImage", !isDeletedImage(), isDeletedImage());
            return;
        }
        firePropertyChange("DeletedImage", !isDeletedImage(), isDeletedImage());
        String codeImage = _ownedImage.getCodeHinhAnh();
        String urlImage = _ownedImage.getDuongDan();
        jtfCodeImage.setText(codeImage);
        jtfURLImage.setText(urlImage);
    }

    // </editor-fold>
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jlbCodeImage = new javax.swing.JLabel();
        jtfCodeImage = new javax.swing.JTextField();
        jlbURLImage = new javax.swing.JLabel();
        jtfURLImage = new javax.swing.JTextField();
        jbtnBrowse = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jbtnSave = new javax.swing.JButton();
        jbtnCancel = new javax.swing.JButton();
        jbtnDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("GUI/Admin/resources/AdminImageDialog"); // NOI18N
        setTitle(bundle.getString("title")); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("jpnDetails"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jlbCodeImage.setText(bundle.getString("jlbCodeImage")); // NOI18N
        jlbCodeImage.setName("jlbCodeImage"); // NOI18N

        jtfCodeImage.setName("jtfCodeImage"); // NOI18N

        jlbURLImage.setText(bundle.getString("jlbUrlImage")); // NOI18N
        jlbURLImage.setName("jlbURLImage"); // NOI18N

        jtfURLImage.setName("jtfURLImage"); // NOI18N

        jbtnBrowse.setText(bundle.getString("jbtnBrowse")); // NOI18N
        jbtnBrowse.setName("jbtnBrowse"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlbURLImage)
                    .addComponent(jlbCodeImage))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfCodeImage)
                    .addComponent(jtfURLImage, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                .addContainerGap(39, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(151, Short.MAX_VALUE)
                .addComponent(jbtnBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(101, 101, 101))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbCodeImage)
                    .addComponent(jtfCodeImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbURLImage)
                    .addComponent(jtfURLImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtnBrowse)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("jpActions"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jbtnSave.setText(bundle.getString("jbtnSave")); // NOI18N
        jbtnSave.setName("jbtnSave"); // NOI18N

        jbtnCancel.setText(bundle.getString("jbtnCancel")); // NOI18N
        jbtnCancel.setName("jbtnCancel"); // NOI18N

        jbtnDelete.setText(bundle.getString("jbtnDelete")); // NOI18N
        jbtnDelete.setName("jbtnDelete"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(jbtnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50)
                .addComponent(jbtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnSave)
                    .addComponent(jbtnCancel)
                    .addComponent(jbtnDelete))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jbtnBrowse;
    private javax.swing.JButton jbtnCancel;
    private javax.swing.JButton jbtnDelete;
    private javax.swing.JButton jbtnSave;
    private javax.swing.JLabel jlbCodeImage;
    private javax.swing.JLabel jlbURLImage;
    private javax.swing.JTextField jtfCodeImage;
    private javax.swing.JTextField jtfURLImage;
    // End of variables declaration//GEN-END:variables
}
